using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace TrovaIntruso.core
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public abstract class Screen : Microsoft.Xna.Framework.DrawableGameComponent
    {

        public List<DrawableGameComponent> Components=new List<DrawableGameComponent>();
        public Screen(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
        }

        protected override void LoadContent()
        {
            base.LoadContent();
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here
            foreach (DrawableGameComponent dg in Components) { dg.Initialize(); }
            base.Initialize();
        }

        /// <summary>
        /// add a Component to this screen
        /// </summary>
        /// <param name="comp"></param>
        public void AddComponent(DrawableGameComponent comp) {
            Components.Add(comp);
        }

        /// <summary>
        /// Remove component from screen
        /// </summary>
        /// <param name="comp"></param>
        public void RemoveComponent(DrawableGameComponent comp)
        {
            Components.Remove(comp);
        }
        public void RemoveComponent(int index)
        {
            Components.RemoveAt(index);
        }

        public void Toggle(bool on) {
            this.Visible = on;
            this.Enabled = on;
        }

        /// <summary>
        /// Freezing the update method but continue draw
        /// </summary>
        public void Freeze()
        {
            this.Enabled = false;
        }

        public void Unfreeze() {
            Enabled = true;
        }

        public void Hide()
        {
            Visible = false;
        }
        public void Show()
        {
            Visible = true;
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            if (Enabled)
            {
                SelfUpdate(gameTime);
                for (int i = 0; i < Components.Count; i++) {
                    Components[i].Update(gameTime);
                }
            }

            base.Update(gameTime);
        }

        protected virtual void SelfUpdate(GameTime gt) { 
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            if (Visible)
            {
                SelfDraw(gameTime);
                for (int i = 0; i < Components.Count; i++)
                {
                    Components[i].Draw(gameTime);
                }
            }
            
        }

        protected virtual void SelfDraw(GameTime gt) { 
        
        }

    }
}
